package com.webdev.base.service.impl;

import com.auth0.jwt.JWT;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.webdev.base.mapper.AdminMapper;
import com.webdev.base.pojo.Admin;
import com.webdev.base.service.IAdminService;
import com.webdev.base.util.Constants;
import com.webdev.base.util.Md5Generator;
import com.webdev.base.vo.AdminVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public AdminVo login(String username, String password) {
        AdminVo admin = adminMapper.getAdminByNameAndPassword(username, Md5Generator.getMD5String(password));
        log.info("admin info:" + admin);
        return admin;
    }

    @Override
    public Admin getUserByToken(String token) {
        if (token == null) return null;
//        return (User) redisTemplate.opsForValue().get(Constants.REDIS_USERTOKEN + token);
        Integer id = JWT.decode(token).getClaims().get(Constants.TOKEN_USERID).asInt();
        if (id == null) return null;
        log.info("user_id:" + id);
        return adminMapper.getUserById(id);
    }
}
